<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-card
      shadow="never"
      class="search-wrapper"
    >
      <el-form
        :model="queryParams"
        inline
      >
        <el-form-item label="字典名称">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入字典名称"
            clearable
          />
        </el-form-item>

        <el-form-item label="字典类型">
          <el-input
            v-model="queryParams.type"
            placeholder="请输入字典类型"
            clearable
          />
        </el-form-item>

        <el-form-item label="状态">
          <el-select
            v-model="queryParams.status"
            placeholder="全部状态"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            @click="handleQuery"
          >
            搜索
          </el-button>
          <el-button @click="resetQuery">重置</el-button>
          <el-button
            type="primary"
            @click="openDialog('create')"
          >
            新增
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card shadow="never">
      <el-table
        v-loading="loading"
        :data="tableData"
        border
      >
        <el-table-column
          prop="id"
          label="ID"
          width="80"
        />

        <el-table-column
          prop="name"
          label="字典名称"
        />

        <el-table-column
          prop="type"
          label="字典类型"
        />

        <el-table-column
          prop="status"
          label="状态"
          width="100"
        >
          <template #default="scope">
            <el-tag :type="scope.row.status === 0 ? 'success' : 'danger'">
              {{ scope.row.status === 0 ? '启用' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          prop="remark"
          label="备注"
        />

        <el-table-column
          label="操作"
          width="220"
        >
          <template #default="scope">
            <el-button
              size="small"
              @click="openDialog('update', scope.row.id)"
            >
              修改
            </el-button>
            <el-button
              size="small"
              @click="navigateToData(scope.row.type)"
            >
              数据
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        style="margin-top: 30px"
        v-model:current-page="queryParams.pageNo"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next"
        @size-change="getList"
        @current-change="getList"
      />
    </el-card>

    <!-- 表单弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="80px"
      >
        <el-form-item
          label="字典名称"
          prop="name"
        >
          <el-input v-model="formData.name" />
        </el-form-item>

        <el-form-item
          label="字典类型"
          prop="type"
        >
          <el-input
            v-model="formData.type"
            :disabled="!!formData.id"
          />
        </el-form-item>

        <el-form-item
          label="状态"
          prop="status"
        >
          <el-radio-group v-model="formData.status">
            <el-radio :label="0">启用</el-radio>
            <el-radio :label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item
          label="备注"
          prop="remark"
        >
          <el-input
            v-model="formData.remark"
            type="textarea"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click="submitForm"
        >
          确定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getDictTypePage,
  createDictType,
  updateDictType,
  deleteDictType,
  getDictType
} from '@/api/dict/dict.type'
import { useRouter } from 'vue-router'

interface DictType {
  id?: number
  name: string
  type: string
  status: number
  remark: string
}

const router = useRouter()

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: '',
  type: '',
  status: undefined
})

const tableData = ref<DictType[]>([])
const total = ref(0)
const loading = ref(false)
const dialogVisible = ref(false)
const dialogTitle = ref('')
const formData = ref<DictType>({
  name: '',
  type: '',
  status: 1,
  remark: ''
})
const formRef = ref()
const statusOptions = [
  { value: 0, label: '启用' },
  { value: 1, label: '停用' }
]

const formRules = {
  name: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
  type: [{ required: true, message: '请输入字典类型', trigger: 'blur' }],
  status: [{ required: true, message: '请选择状态', trigger: 'change' }]
}

// 获取数据列表
const getList = async () => {
  loading.value = true
  try {
    const res = await getDictTypePage(queryParams)
    tableData.value = res.list
    total.value = res.total
  } finally {
    loading.value = false
  }
}

// 搜索
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

// 重置查询
const resetQuery = () => {
  queryParams.pageNo = 1
  queryParams.name = ''
  queryParams.type = ''
  queryParams.status = undefined
  getList()
}

// 打开弹窗
const openDialog = async (type: string, id?: number) => {
  dialogTitle.value = type === 'create' ? '新增字典类型' : '修改字典类型'
  dialogVisible.value = true

  if (type === 'update' && id) {
    const res = await getDictType(id)
    formData.value = res
  } else {
    formData.value = { name: '', type: '', status: 1, remark: '' }
  }
}

// 提交表单
const submitForm = async () => {
  await formRef.value.validate()

  if (formData.value.id) {
    await updateDictType(formData.value)
    ElMessage.success('修改成功')
  } else {
    await createDictType(formData.value)
    ElMessage.success('新增成功')
  }

  dialogVisible.value = false
  getList()
}

// 删除
const handleDelete = async (id: number) => {
  await ElMessageBox.confirm('确认删除该字典类型？')
  await deleteDictType(id)
  ElMessage.success('删除成功')
  getList()
}

const navigateToData = (type: string) => {
  router.push(`/dictData?type=${type}`)
}

// 初始化加载
onMounted(getList)
</script>

<style scoped>
.search-wrapper {
  margin-bottom: 20px;
}
</style>
